<template>
  <view class="index">
    <!-- 自定义导航栏 -->
    <CustomNavbar class="naverbar"> </CustomNavbar>
    <scroll-view
      class="scrollviewi"
      scroll-y
      @refresherrefresh="onRefresherrefresh"
      refresher-enabled
      show-scrollbar
      :refresher-triggered="isTriggered"
      @scrolltolower="onScrolltolower"
    >
      <!-- 统用轮播组件 v-if="isChildReady" -->
      <XtxSwiper :list="bannerList"></XtxSwiper>
      <!--首页分类 -->
      <CategoryPanel :list="categoryList"></CategoryPanel>
      <!--热门推荐 -->
      <HotPanel :list="hotItemList"></HotPanel>
      <!-- 猜你喜欢 -->
      <XtxGuess ref="guessRef"></XtxGuess>
    </scroll-view>
  </view>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import CustomNavbar from '@/pages/index/components/CustomNavbar.vue'
import CategoryPanel from '@/pages/index/components/CategoryPanel.vue'
import HotPanel from '@/pages/index/components/HotPanel.vue'
import { getGuessAPI, getHomeBannerAPI, getHomeCategoryAPI, getHomeHotAPI } from '@/services/home'
import type { BannerItem, CategoryItem, HomeItem } from '@/types/home.d.ts'
import { onLoad, onReachBottom } from '@dcloudio/uni-app'
import type { XtxGuessInstance } from '@/types/component.d.ts'
// 获取banner

const bannerList = ref<BannerItem[]>([])
const categoryList = ref<CategoryItem[]>([])
const hotItemList = ref<HomeItem[]>([])
// 获取自定义XtxGuess组件实例
const guessRef = ref<XtxGuessInstance>()
const isTriggered = ref(false)

// const isChildReady = ref(false);

onLoad(() => {
  getHomeBanner()
  getHomeCategory()
  getHomeHot()
})
const getHomeBanner = async () => {
  let res = await getHomeBannerAPI()
  bannerList.value = res.result
  // isChildReady.value = true
  // console.log(bannerList.value)
}
const getHomeCategory = async () => {
  const res = await getHomeCategoryAPI()
  categoryList.value = res.result
}

// 热门推荐
const getHomeHot = async () => {
  const res = await getHomeHotAPI()
  hotItemList.value = res.result
  console.log(hotItemList.value)
}

// 下拉触底
const onScrolltolower = () => {
  console.log('1111111111')
  //  await  guessRef.value?.getMore()
}
// 猜你喜欢触底  scroll-view组件自带触发
const onRefresherrefresh = async () => {
  // 获取子组件的实例的方法
  await guessRef.value?.resetData()
  // 开启动画
  isTriggered.value = true
  console.log('下拉1')

  // 依串行执行
  // await  getHomeBanner()
  // await  getHomeCategory()
  // await getHomeHot()
  // 并行
  await Promise.all([getHomeBanner(), getHomeCategory(), getHomeHot(), guessRef.value?.getMore()])

  //关闭动画
  isTriggered.value = false
}

onReachBottom(() => {
  guessRef.value?.getMore()
  // console.log("底部")
})
</script>

<style lang="scss" scoped>
.index {
  display: flex;
  flex-direction: column;
  background-color: #f7f7f7;
  //  height: 100%;
  //  overflow: hidden;
  //  margin-top: 168rpx;
  position: relative;
  .naverbar {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    z-index: 999;
  }
  .scrollviewi {
    margin-top: 165rpx;
    flex-grow: 1;
    box-sizing: border-box;
    overflow-y: auto;
  }
}
</style>
